package com.xmb.wechat.widget;

import android.app.Activity;
import android.app.Dialog;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.xmb.wechat.R;

public class InputDialog {

    /**
     * @param act
     * @param title
     * @param defaultText
     * @param inputType   EditorInfo.TYPE_CLASS_TEXT
     * @param lsn
     */
    public static void showInputDialog(final Activity act, String title, String defaultText, int inputType, final OnInputDialogListener lsn) {
        AlertDialog.Builder builder = new AlertDialog.Builder(act);
        View view = LayoutInflater.from(act).inflate(R.layout.wechat_input_dialog, null);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        tvTitle.setText(title);
        TextView btnOK = view.findViewById(R.id.btn_ok);
        TextView btnCancel = view.findViewById(R.id.btn_cancel);
        final EditText etInput = view.findViewById(R.id.et_input);
        etInput.setInputType(inputType);
        if (defaultText != null)
            etInput.setText(defaultText);
        builder.setView(view);
        final Dialog dialog = builder.create();
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                lsn.onCancelClicked();
            }
        });
        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //                dialog.dismiss();
                lsn.onOKClicked(dialog, etInput.getText().toString());
            }
        });
        dialog.show();
    }

    public interface OnInputDialogListener {
        void onCancelClicked();

        void onOKClicked(Dialog dialog, String inputText);
    }

}
